<template>
	<view>
		<u-toast ref="uToast" />
		<view class="button">
			<view class="delete_button">
				<u-button type="error" text="删除账户" size="normal" plain @click="deleteAccount()"></u-button>
			</view>
		</view>
		<view class="title">
			<view class="title-text">
				<text>{{title_text}}的余额</text>
			</view>
			<view class="title-money">
				<u-icon name="rmb" size="40" color="#303133"></u-icon>
				<text>{{money}}</text>
			</view>
		</view>
		
		<view class="in-out-flow">
			<view class="inflow">
				<text style="color: #82848a;">流入:</text>
				<text>{{inflow}}</text>
			</view>
			<view class="outflow">
				<text style="color: #82848a;">流出:</text>
				<text>{{outflow}}</text>
			</view>
		</view>
		
		<view>
			<u-divider lineColor="#82848a" :dot="true"></u-divider>
		</view>
		
		<view class="checkbox">
			<u-subsection 
				:list="dividerList" 
				:current="current" 
				mode="subsection"
				activeColor="#007AFF"
				@change="sectionChange"
				fontSize="17"
				></u-subsection>
		</view>
		<view class="divider">
			<u-divider lineColor="#82848a"></u-divider>
		</view>
		
		<view v-show="current == 0">
			<view class="cell-group" v-for="(item1, index1) in list" :key="index1">
				<view class="cell-icon">
					<u-icon :name="item1.typeIcon" size="28" color="#2979ff" ></u-icon>
				</view>
				<view class="cell-item" @click="getDetailInfo(index1)">
					<view class="cell-info">
						<view class="info-text">
							<text>{{item1.billType}}</text>
						</view>
						<view class="info-remarks">
							<text>{{item1.billDate}}&nbsp;&nbsp;{{item1.billTime}}</text>
						</view>
					</view>
					<view class="cell-money">
						<text>{{[item1.state == 1 ? '-' : '+']+item1.money}}</text>
					</view>
				</view>
			</view>
		</view>

		<view v-show="current == 1">
			<view class="cell-group" v-for="(item1, index1) in list" :key="index1">
				<view class="cell-icon">
					<u-icon name="/static/account/info/borrow.png" size="28" color="#2979ff" ></u-icon>
				</view>
				<view class="cell-item">
					<view class="cell-info">
						<view class="info-text">
							<text v-if="item1.state == 1">借给{{item1.person}}</text>
							<text v-else>向{{item1.person}}借入</text>
						</view>
						<view class="info-remarks">
							<text>{{item1.borrowDate}}&nbsp;&nbsp;{{item1.borrowTime}}</text>
						</view>
					</view>
					<view class="cell-money">
						<text>{{[item1.state == 1 ? '-' : '+']+item1.borrowMoney}}</text>
					</view>
				</view>
			</view>
		</view>
		
		<view v-show="current == 2">
			<view class="cell-group" v-for="(item1, index1) in list" :key="index1">
				<view class="cell-icon">
					<u-icon name="/static/account/info/transfer.png" size="28" color="#2979ff" ></u-icon>
				</view>
				<view class="cell-item">
					<view class="cell-info">
						<view class="info-text">
							<text v-if="item1.transferState == 1">转出至{{item1.toName}}账户</text>
							<text v-else>从{{item1.toName}}转入</text>
						</view>
						<view class="info-remarks">
							<text>{{item1.transferDate}}&nbsp;&nbsp;{{item1.transferTime}}</text>
						</view>
					</view>
					<view class="cell-money">
						<text>{{[item1.transferState == 1 ? '-' : '+']+item1.transferMoney}}</text>
					</view>
				</view>
			</view>
		</view>

		<view>
			<u-popup :show="show" @close="close" @open="open" mode="center" :round="15">
				<view style="height: 400rpx;">
					<view class="popup_info">
						<view class="tip">
							<text>提示</text>
						</view>
						<view class="tip_info">
							<text>删除该账户后，该账户的相关收支信息和转账记录将会删除。确定要删除吗？</text>
						</view>
					</view>
					<view class="popup_button">
						<view class="left_button">
							<u-button type="primary" text="取消" size="normal" @click="cancelDelete()"></u-button>
						</view>
						<view class="right_button">
							<u-button type="error" text="删除" size="normal" @click="confirmDelete()"></u-button>
						</view>
					</view>
				</view>
			</u-popup>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				current: 0,
				title_text: "",
				money: 0,
				outflow: 0,
				inflow: 0,
				state: 1,
				billDate: "",
				billType: "",
				dividerList: [{"value": 0,"name": "收支"	},{"value": 1,"name": "借款"},{"value": 2,"name": "转账"}],
				accountId: '',
				list: []
			}
		},
		onShow() {
			
		},
		onLoad(options) {
			let data = JSON.parse(options.data)
			console.log(data)
			this.accountId = data.accountId;
			this.title_text = data.accountName;
			this.getAccountBill(uni.getStorageSync('userId'), this.accountId, this.current)
			this.AllInOutflow()
		},
		methods: {
			sectionChange(index){
				this.current = index;
				this.getAccountBill(uni.getStorageSync('userId'), this.accountId, this.current)
			},
			getDetailInfo (num) {
				let data = this.list[num]
				uni.navigateTo({
					url: '/pages/detailInfo/detailInfo?data='+JSON.stringify(data)
				})
			},
			getAccountBill(userId, accountId, listType) {
				let params = {
					"userId": userId,
					"accountId": accountId,
					"listType": listType
				}
				const _this = this
				uni.request({
					header: {
						  'Content-Type': 'application/x-www-form-urlencoded',
						  'token': uni.getStorageSync('token') 
					   }, 
					url: this.$USER_AVATOR_URL.USER_AVATOR_URL+"/account/loadaccountbill",
					method: 'POST',
					data: params,
					success(res) {
						console.log(res)
						_this.list = res.data.data
					}
				})
			},
			deleteAccount() {
				this.show = true
			},
			cancelDelete() {
				this.show = false
			},
			confirmDelete() {
				let params = {
					"userId": uni.getStorageSync('userId'),
					"accountId": this.accountId
				}
				const _this = this
				uni.request({
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						  'token': uni.getStorageSync('token')
					},
					url: this.$USER_AVATOR_URL.USER_AVATOR_URL+"/account/deleteaccount",
					method: 'POST',
					data: params,
					success(res) {
						_this.show = false
						if (res.data.code == 200) {
							_this.$refs.uToast.show({
								type: 'success',
								title: '操作成功',
								message: res.data.errorMessage,
								duration: 700,
								complete() {
									uni.switchTab({
										url: '/pages/account/account',
										success: (res) => {
											let page = getCurrentPages().pop();
											if (page == undefined || page == null) return;
											page.onLoad();
										}
										
									})
								},
								
							})
						}else{
							_this.$refs.uToast.show({
								type: 'error',
								title: '操作失败',
								message: res.data.errorMessage,
								duration: 1000,
							})
						}					
					}
				})
			},
			AllInOutflow() {
				let params = {
					"userId": uni.getStorageSync('userId'),
					"accountId": this.accountId
				}
				const _this = this
				uni.request({
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						  'token': uni.getStorageSync('token')
					},
					url: this.$USER_AVATOR_URL.USER_AVATOR_URL+"/account/allinoutflow",
					method: 'POST',
					data: params,
					success(res) {
						_this.money = res.data.data.accountMoney
						_this.inflow = res.data.data.inflow
						_this.outflow = res.data.data.outflow
					}
				})
			},
			open() {
			  this.show = true
			},
			close() {
			  this.show = false
			}
		},


	}
</script>

<style>
	
	.button{
		display: flex;
		justify-content: flex-end;
		align-items: center;
		
		margin-right: 30rpx;
		margin-top: 30rpx;
	}
	.delete-button{
		
		width: 100rpx;
		height: 50rpx;

	}
	
	.title {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		margin-top: 10rpx;
	}
	.title-text {
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 35rpx;
		color: #82848a;
	}
	.title-money {
		display: flex;
		justify-content: flex-end;
		align-items: center;
		font-size: 80rpx;
		font-weight: bolder;
		margin-top: 15rpx;
	}
	
	.in-out-flow {
		font-size: 40rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 20rpx;
	}
	.inflow {
		width: 350rpx;
		display: flex;
		justify-content: center;
	}
	.outflow {
		width: 350rpx;
		display: flex;
		justify-content: center;
	}
	
	.checkbox{
		margin-left: 150rpx;
		margin-right: 150rpx;
	}
	.divider{
		margin-top: 30rpx;
		margin-left: 30rpx;
		margin-right: 30rpx;
		margin-bottom: 40rpx;
	}
	
	.cell-group {
		display: flex;
		flex-wrap: wrap;
		margin-top: 25rpx;
		margin-left: 30rpx;
		margin-right: 20rpx;
	}
	.cell-icon {
		width: 85rpx;
		height: 85rpx;
		display: flex;
		align-items: center;
		background-color: #2979ff;
		
		border-radius: 100%;
		box-sizing: border-box;
		justify-content: center;
		align-items: center;
	}
	.cell-item {
		height: 100rpx;
		display: flex;
		flex-wrap: wrap;
		border-bottom: #c8c9cc;
		border-style: hidden hidden solid hidden;
		border-width: 1rpx;
	}
	.cell-info {
		width: 380rpx;
		display: flex;
		justify-content: flex-start;
		flex-wrap: wrap;
		margin-left: 20rpx;
	}
	.info-text {
		font-size: 35rpx;
		width: 400rpx;
		height: 60rpx;
	}
	.info-remarks {
		width: 400rpx;
		color: #909399;
	}
	.cell-money {
		width: 200rpx;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		
		font-size: 40rpx;
		font-weight: bolder;
	}
	
	.popup_button {
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.left_button {
		width: 200rpx;
		height: 100rpx;
		margin: 50rpx;
	}
	.right_button {
		width: 200rpx;
		height: 100rpx;
		margin: 50rpx;
	}
	
	.popup_info {
		display: flex;
		flex-wrap: wrap;
		justify-content: center;
		align-items: center;
		margin-top: 30rpx;
	}
	.tip {
		display: flex;
		justify-content: center;
		align-items: center;
		font-weight: bolder;
		font-size: 40rpx;
		width: 100px;
	}
	.tip_info {
		display: flex;
		justify-content: center;
		align-items: center;
		color: #909399;
		width: 300px;
		margin-top: 30rpx;
		font-size: 35rpx;
	}
</style>
